//nextTick 2.4版本之前源码
let callbacks = [];
let pending = false;
function flushCallbacks(){
    callbacks.forEach(cb=>cb());
    pending = false;
    callbacks = [];
}


let timerFunc;
if (Promise) { // then方法是异步的
    timerFunc = () => {
        Promise.resolve().then(flushCallbacks)
    }
}else if (MutationObserver) { // MutationObserver 也是一个异步方法
    let observe = new MutationObserver(flushCallbacks); // H5的api
    let textNode = document.createTextNode(1);
    observe.observe(textNode, {
        characterData: true
    });
    timerFunc = () => {
        textNode.textContent = 2;
    }
}else if (setImmediate) {
    timerFunc = () => {
        setImmediate(flushCallbacks)
    }
}else{
    timerFunc = () => {
        setTimeout(flushCallbacks, 0);
    }
}

export function nextTick(cb) {
    callbacks.push(cb);
    if(pending === false){
        timerFunc();
        pending = true;
    }
}
